
#ifndef _MEMBER_BSHAPE_H_
#define _MEMBER_BSHAPE_H_

#include <BPair>
#include <BMatrix>
#include <BMatrix>
#include <BShape>
#include <BMaterial>
#include <BTexture>
#include <BProgram>

#define shape_member(shape)		((member_BShape*)((BShape*)shape)->_ptr)

namespace BWE
{
	inline int MaxDot(const BVector& direct, const BVector* array, int count, BReal& maxDot)
	{
		maxDot = -REAL_MAX;
		int index = -1;
		for (int i = 0; i < count; i++)
		{
			BReal dot = array[i].dot(direct);
			if (dot > maxDot)
			{
				maxDot = dot;
				index = i;
			}
		}
		return index;
	}

	class member_BShape : public BObject
	{
		friend class BShape;
	public:
		member_BShape(BShape* shape);
		~member_BShape();

		void slotMaterialEnabled(BObject* object);
		void slotMaterialChanged(BObject* object);

		void slotTextureEnabled(BObject* object);
		void slotTextureDirty(BObject* object);

		void slotProgramEnabled(BObject* object);
		void slotProgramDirty(BObject* object);

		inline void render(BRender& render)
		{
			boss->render(render);
		}
		inline BVector supportVertex(const BVector& direct) const
		{
			BVector vertex = boss->vertex(direct);
			if (margin != 0)
			{
				vertex += direct * margin;
			}
			return vertex;
		}
		inline BVector calculateInertia(BReal mass) const
		{
			return boss->inertia(mass);
		}
		BSpace transSpace(const BMatrix& matrix) const;

		BShape*					boss;
		bool					isolate;
		bool					visible;
		bool					colliding;
		bool					transparent;

		BShapeHolder			proxy;
		BMatrix					matrix;
		BColor					color;
		BMaterialHolder			material;
		BProgramHolder			program;
		BArray<BTextureHolder>	textures;

		bool					normalize;
		bool					lighting;
		bool					depthTest;
		float					depthOffset;
		bool					cullback;
		bool					pointSmooth;
		float					pointSize;
		bool					lineSmooth;
		float					lineWidth;
		bool					polySmooth;
		PolyMode				polyMode;
		BReal					margin;

		BBox					box;
		BSpace					space;
		bool					dirty;
		bool					dirtySpace;
	};
}

#endif